//
//  SunActivityVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/31.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunActivityVC.h"
#import "SunWebVC.h"
#import "SunHtmlWebVC.h"

@interface SunActivityVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tb;
    NSArray *_dataArr;
}

@end

@implementation SunActivityVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"活动中心";
    self.view.backgroundColor = BACKGROUND;
    [self creatUI];
    [self requestToActivity];
    // Do any additional setup after loading the view.
}

- (void)creatUI{
    _tb = [[UITableView alloc]initWithFrame:CGRectNull style:UITableViewStyleGrouped];
    [self.view addSubview:_tb];
    [_tb lt_All:SunEdgesMake(0, X(15), 0, X(15)) save:YES];
    _tb.delegate = self;
    _tb.dataSource = self;
    _tb.backgroundColor = BACKGROUND;
    _tb.showsVerticalScrollIndicator = NO;
}

- (void)requestToActivity{
    [[Sun shareSun]showActivity:self];
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_ACTIVITY];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        [[Sun shareSun] hideAll];
        _dataArr = requestDic;
        [_tb reloadData];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun] hideAll];
        [[Sun shareSun]showMsg:errorInfo];
    }];
}

#pragma mark request
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return Y(15);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == _dataArr.count - 1) {
        return Y(25);
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return Y(190);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    for (int i=0; i<cell.subviews.count; i++) {
        UIView *view = [cell subviews][i];
        [view removeFromSuperview];
        i--;
    }
    cell.layer.borderWidth = 1;
    cell.layer.borderColor = LINE.CGColor;
    cell.clipsToBounds = YES;
    cell.layer.cornerRadius = 5;
    UIImageView *img = [[UIImageView alloc]init];
    [cell addSubview:img];
    [img lt_Top:SunEdgesMake(0, 0, 0, Y(162)) to:nil save:YES];
    [img sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KIMG,[_dataArr[indexPath.section] objectForKey:@"img"]]]];
    UILabel *label = [[UILabel alloc]init];
    [cell addSubview:label];
    [label lt_Bottom:SunEdgesMake(X(15), 0, X(15), Y(28)) to:nil save:YES];
    label.font = [UIFont systemFontOfSize:13];
    [label setMarkup:[NSString stringWithFormat:@"<font color=\"ff5971\">【%@】</font><font color=\"#333333\">%@</font>",[_dataArr[indexPath.section] objectForKey:@"status"],[_dataArr[indexPath.section] objectForKey:@"name"]]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SunWebVC *vc = [[SunWebVC alloc]init];
    vc.urlStr = [_dataArr[indexPath.section] objectForKey:@"param"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
